﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaobaoMatch
{
    public class SpiderDataJob
    {
        public void Execute()
        {
            IDictionary<string, string> dicCate = ListCate(Spider.absUrl, "//div[@class='main2_doc clearfix']/ul/li/a");//1.先获得有多少分类
            AppConfig.DicCate = dicCate;
            int erroCount = 0;
            foreach (var cate in dicCate)
            {
                int total = getTatalPage("http://www.taokezhushou.com/cate/" + cate.Key, "//ul[@class='pagination']/li");//获得这个分类下面总共多少页
                for (int i = 1; i < total; i++)
                {
                    string url = Spider.GetPageUrlForCate(i, cate.Key);
                    IDictionary<string, string> dicLink = GetCurrentPagehref(url, "//div[@class='goods-a']/a");
                    foreach (var link in dicLink)
                    {
                        if (AppConfig.DicProduct.ContainsKey(link.Key))
                            continue;
                        Product product = GetProduct(link.Value);
                        if (product == null)
                        {
                            erroCount++;
                            continue;
                        }
                        product.Cate = cate.Key;
                        product.CateName = cate.Value;
                        product.ID = link.Key;
                        Action<Product> ac = SaveData;
                        ac(product);
                    }
                    break;
                }
            }
        }

        public void ExecuteForTest()
        {
            IDictionary<string, string> dicCate = new Dictionary<string, string>();
            dicCate.Add("1", "女装");
            dicCate.Add("2", "男装");
            dicCate.Add("3", "鞋包");
            dicCate.Add("4", "家具");
            dicCate.Add("5", "文体");
            dicCate.Add("6", "电器");
            dicCate.Add("7", "配饰");
            dicCate.Add("8", "数码");
            dicCate.Add("9", "美食");
            dicCate.Add("10", "美妆");
            dicCate.Add("11", "母婴");
            dicCate.Add("12", "其他");

            AppConfig.DicCate = dicCate;

            int i = 0;
            int k = 0;
            foreach (var item in AppConfig.DicCate)
            {
                i++;

                for (int j = 0; j < 30; j++)
                {
                    k++;
                    Product p = new Product();
                    p.ImageUrl = "http://acdn.taokezhushou.com/c62b82318c91ab34a90d377a152703b3.jpg@1e_1c_0o_0l_287h_287w_100q.src";
                    p.LocalImageUrl = "";
                    p.Title = "透蜜明眸眼霜霜 淡化眼袋黑眼圈脂肪粒" + j;
                    p.CurrentPrice = 69;
                    p.DiscountPrice = 50.00;
                    p.AfterPrice = 19;
                    p.ProductDetail = "https://detail.tmall.com/item.htm?id=539108381016";
                    p.DiscountUrl = "http://shop.m.taobao.com/shop/coupon.htm?seller_id=850069620&activity_id=dc17ce231efa4c16b4c8768fc11b9f8a";
                    p.Cate = item.Key;
                    p.CateName = item.Value;
                    p.ID = k.ToString();
                    p.CreateTime = DateTime.Now;
                    p.Description = "淡化眼圈眼袋脂肪粒，焕发肌肤容颜，眼周立体显年轻。";
                    p.Rule = "*单笔满65.00元可用，每人限领1张";
                    p.SurplusAmount = 2942;
                    p.UsedAmount = 58;
                    p.ExpiredTime = DateTime.Now;
                    p.SaleAmount = 100000 + k;
                    p.TotalAmount = 3000;
                    AppConfig.DicProduct.Add(p.ID.ToString(), p);
                }




            }


        }
        #region private

        private void SaveData(Product product)
        {
            AppConfig.DicProduct.Add(product.ID, product);
        }

        private static int getTatalPage(string urls, string xpath)
        {
            HtmlAgilityPack.HtmlNode nodes = Spider.GetHtmlNodeFromLink(urls);
            return Spider.GetTotalPages(nodes, xpath);
        }

        private static IDictionary<string, string> GetCurrentPagehref(string currentUrl, string xpath)
        {
            HtmlAgilityPack.HtmlNode nodes = Spider.GetHtmlNodeFromLink(currentUrl);
            return Spider.ListCurrentPageUrl(nodes, xpath);

        }

        private static IDictionary<string, string> ListCate(string url, string xpath)
        {
            HtmlAgilityPack.HtmlNode nodes = Spider.GetHtmlNodeFromLink(url);
            return Spider.ListCate(nodes, xpath);
        }

        private static Product GetProduct(string currentUrl)
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("ImageUrl", "/html/body/div[3]/div[1]/div[2]/div[1]/a/img");
            dic.Add("Title", "/html/body/div[3]/div[1]/div[2]/div[2]/div[1]/div/h3");
            dic.Add("CurrentPrice", "/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/ul/li[2]");
            dic.Add("DiscountPrice", "/html/body/div[3]/div[1]/div[2]/div[2]/div[3]/p[1]/span");
            dic.Add("AfterPrice", "/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/ul/li[1]/span");
            dic.Add("ProductDetail", "/html/body/div[3]/div[1]/div[2]/div[2]/div[5]/div[1]/p[2]/a");
            dic.Add("DiscountUrl", "/html/body/div[3]/div[1]/div[2]/div[2]/div[5]/div[1]/p[1]/a[2]");
            dic.Add("Description", "/html/body/div[3]/div[1]/div[2]/div[2]/div[1]/p");
            dic.Add("Rule", "/html/body/div[3]/div[1]/div[2]/div[2]/div[3]/p[2]");
            dic.Add("Other", "/html/body/div[3]/div[1]/div[2]/div[2]/div[4]/p[1]");
            dic.Add("SaleAmount", "/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/ul/li[3]");


            HtmlAgilityPack.HtmlNode nodes = Spider.GetHtmlNodeFromLink(currentUrl);
            return Spider.GetProduct(nodes, dic);
        }

        #endregion
    }

    public class MyJobRunner
    {
        public virtual void Run()
        {
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();
            JobDetail job = new JobDetail("job1", "group1", typeof(SpiderDataJob));
            CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1");
            trigger.CronExpressionString = "0 0/30 * * * ?";
            sched.AddJob(job, true);
            DateTime ft = sched.ScheduleJob(trigger);

            sched.Start();
        }
    }
}
